using System;
using System.Collections.Generic;
using DAL;
using Entity;
using Utilities;

namespace Business
{
	public class InUserBusiness
	{
		#region code CMS
        public static List<InUser> GetInUserList()
        {
            var result = FreshCmsDataAccessProvider.InUser.GetInUserList();
            if (result != null && result.Count > 0) ReFomatInUser(result);
            return result;
        }

        public static List<InUser> GetInUserListSearch(InUser InUser)
        {
            var result = FreshCmsDataAccessProvider.InUser.GetInUserListSearch(InUser);
            if (result != null && result.Count > 0) ReFomatInUser(result);
            return result;
        }

        //public static List<InUser> GetInUserListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        //{
        //    var result = FreshCmsDataAccessProvider.InUser.GetInUserListByPaging(pageIndex, pageSize, ref totalRecord);
        //    return result;
        //}

        public static List<InUser> GetInUserListByGroupId(int groupid)
        {
            var result = FreshCmsDataAccessProvider.InUser.GetInUserListByGroupId(groupid);
            if (result != null && result.Count > 0) ReFomatInUser(result);
            return result;
        }

        public static InUser GetInUserById(int userId)
        {
            var result = FreshCmsDataAccessProvider.InUser.GetInUserById(userId);
            if (result != null)
            {
                result.UserAvatar = ImageUtils.GetImageThumb(result.UserAvatar, Constant.InUserAvatarWidth, Constant.InUserAvatarHeight, Constant.InUserAvatarDefault);
            }
            return result;
        }

        public static int InsertInUser(InUser InUser)
        {
            var result = FreshCmsDataAccessProvider.InUser.InsertInUser(InUser);
            return result;
        }

        public static int UpdateInUser(InUser InUser)
        {
            var result = FreshCmsDataAccessProvider.InUser.UpdateInUser(InUser);
            return result;
        }

        public static int DeleteInUser(int userId)
        {
            var result = FreshCmsDataAccessProvider.InUser.DeleteInUser(userId);
            return result;
        }

        public static int CheckInUserEmailExist(string email)
        {
            var result = FreshCmsDataAccessProvider.InUser.CheckInUserEmailExist(email);
            return result;
        }

        public static InUser CheckLogin(string email, string pass)
        {
            pass = Utils.MD5Encrypt(pass);
            var result = FreshCmsDataAccessProvider.InUser.CheckLogin(email, pass);
            return result;
        }

        public static InUser GetSessionInUser()
        {
            try
            {
                if (System.Web.HttpContext.Current.Session["User"] != null)
                {
                    return (InUser)System.Web.HttpContext.Current.Session["User"];
                }
            }
            catch (Exception)
            {
                return null;
                throw;
            }
            return null;
        }

		#endregion


        private static void ReFomatInUser(List<InUser> data)
        {
            foreach (var item in data)
            {
                item.UserAvatar = ImageUtils.GetImageThumb(item.UserAvatar, Constant.InUserAvatarWidth, Constant.InUserAvatarHeight, Constant.InUserAvatarDefault);
            }
        }
    }
}